package cn.itrip.auth.service;

import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisAPI;
import cn.itrip.dao.user.ItripUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private MailService mailService;
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private  SmsService smsService;

    public void itriptxCreateUser(ItripUser user) throws Exception {

        //1.添加用户信息
        itripUserMapper.insertItripUser(user);
        //2.生成MD5激活码
        String activeCode = MD5.getMd5(new Date().toLocaleString(),32);
        //3.发送邮件
        mailService.sendActivationMail(user.getUserCode(),activeCode);
        //4.激活码存入Redis
      //  redisAPI.set("activation:111@qq.com",30*60,"b9a4a9c9a4d47d3397349912278a706b")
        redisAPI.set("activation:"+user.getUserCode(),30*60,activeCode);
    }

    public boolean activate(String email, String code) throws Exception {
        String key="activation:"+email;
        if(redisAPI.exist(key)){
            if(redisAPI.get(key).equals(code)){
                //根据userCode查找用户
                Map<String, Object> param=new HashMap<String,Object>();
                param.put("userCode", email);
                List<ItripUser> list= itripUserMapper.getItripUserListByMap(param);
                if(list.size()>0){
                    ItripUser user=list.get(0);
                    //修改用户信息
                    user.setActivated(1);//激活用户
                    user.setUserType(0);//自注册用户
                    user.setFlatID(user.getId());
                    itripUserMapper.updateItripUser(user);
                    return true;
                }
            }
        }
        return false;
    }


    public ItripUser findByUserCode(String userCode)  throws Exception  {
        Map<String, Object> param=new HashMap();
        param.put("userCode", userCode);
        List<ItripUser> list= itripUserMapper.getItripUserListByMap(param);
        if(list.size()>0)
            return list.get(0);
        else
            return null;
    }

    public void itriptxCreateUserByPhone(ItripUser user) throws Exception {
        //1.添加用户信息
        itripUserMapper.insertItripUser(user);
        //2.生成验证码
        String code=String.valueOf(MD5.getRandomCode());
        //3.发送短信
        smsService.send(user.getUserCode(),"1",new String[]{code,"5"});
        //4.激活码存入Redis
        redisAPI.set("activation:"+user.getUserCode(),5*60,code);
    }

    public boolean validatePhone(String phoneNumber, String code) throws Exception {
        String key="activation:"+phoneNumber;
        if(redisAPI.exist(key))
            if(redisAPI.get(key).equals(code)){
                ItripUser user=this.findByUserCode(phoneNumber);
                if(EmptyUtils.isNotEmpty(user))
                {
                    user.setActivated(1);//激活用户
                    user.setUserType(0);//自注册用户
                    user.setFlatID(user.getId());
                    itripUserMapper.updateItripUser(user);
                    return true;
                }
            }
        return false;
    }
}
